package com.css.apps.base.recycle.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.recycle.model.RTrash;
import com.css.apps.base.recycle.service.ITrashBizHandler;
import com.css.common.PluginBus;
import com.css.common.exception.ExceptionFactory;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

/**
 * <p>删除回收站记录</p>
 * @author CSS
 *
 */
@SuppressWarnings("all")
public class DelRecycle extends AdminAction {
	private static Log log = LogFactory.getLog(DelRecycle.class);
	private final String funcid = "ACL_DELRECYCLE";
	//回收站记录uuid
	private String ids;
 
	public DelRecycle() {
		setFuncid(funcid);
	}

	public String adminGo() {
		TransactionCache tx = null;
		try{
			if(StringHelper.isEmpty(ids)){
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));	
				return Action.ERROR;		
			}
			List<String> lst = StringHelper.strToList(ids);
			List<RTrash> itemLst = QueryCache.idToObj(RTrash.class, lst);
			
			tx = new TransactionCache();
//			String entityId = null;
			String recordId = null;
			for (RTrash item : itemLst) {
				recordId = item.getRecordId();
//				entityId = item.getEntityId();
//				rTrashContent = QueryCache.get(RTrashContent.class, entityId);
				ITrashBizHandler bizHandler = PluginBus.getPlugin(ITrashBizHandler.class, item.getFuncId());
				if (bizHandler != null) {
					if (!bizHandler.delEntity(tx, recordId)) {
						throw ExceptionFactory.makeBaseException(Messages.getString("systemMsg.exception"));
					}
				}
				/*
				if (item.getFuncId().equals(Constants.SYS_FW)) {
//					FwForm fwForm = QueryCache.get(FwForm.class, item.getRecordId());
//					tx.delete(fwForm);
					if (!FwService.delFwAll(tx, recordId)) {
						throw ExceptionFactory.makeBaseException(Messages.getString("systemMsg.exception"));
					}
				} else if (item.getFuncId().equals(Constants.SYS_SW)) {
//					SwForm swForm = QueryCache.get(SwForm.class, item.getRecordId());
//					tx.delete(swForm);
					if (!SwService.delSwAll(tx, recordId)) {
						throw ExceptionFactory.makeBaseException(Messages.getString("systemMsg.exception"));
					}
				} else if (item.getFuncId().equals(Constants.SYS_DB)) {
					if (!SuperviseService.delDbAll(tx, recordId)) {
						throw ExceptionFactory.makeBaseException(Messages.getString("systemMsg.exception"));
					}
				}
				*/
				tx.delete(item);
			}
			tx.commit();
			
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		} finally {
		}
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
